package org.tmall.utils;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class PageTag extends SimpleTagSupport {
	
	private PageBean pageBean;
	
	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	@Override
	public void doTag() throws JspException, IOException {
		if (pageBean == null) {
			return;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<div class=\"paging\">");
		sb.append("<a href=\"javascript:void(0)\" onclick=\"go(1);\">首页</a>");
		sb.append("<a href=\"javascript:void(0)\" onclick=\"go("+pageBean.getPrevPage()+");\">上一页</a>");
		for(int i = 1; i<=pageBean.getTotalPages(); i++) {
			String classname = "";
			if (i == pageBean.getPageNo()) {
				classname = "active";
			}
			sb.append("<a href=\"javascript:void(0)\" class=\""+classname+"\" onclick=\"go("+i+");\">"+i+"</a>");
		}
		sb.append("<a href=\"javascript:void(0)\" onclick=\"go("+pageBean.getNextPage()+");\">下一页</a>");
		sb.append("<a href=\"javascript:void(0)\" onclick=\"go("+pageBean.getTotalPages()+");\">末页</a>");
		sb.append("<span>总共"+pageBean.getTotalCount()+"条记录,当前显示"+pageBean.getStart()+"-"+pageBean.getEnd()+"条数据</span>");
		sb.append("<input type='hidden' id='pageNo' name='p'/>");
		sb.append("</div>");
		
		//打印一段JavaScript脚本
		sb.append("<script type=\"text/javascript\">");
		sb.append("function go(page){document.getElementById(\"pageNo\").value = page;document.getElementById(\"form1\").submit();}");
		sb.append("</script>");
		this.getJspContext().getOut().print(sb.toString());
	}
}





